﻿@inherits InputBase<string>

@if (Rows > 0)
{
    <textarea rows="@Rows"
           class="input bg-dark-550 @Class"
           style="@Style"
           readonly="@IsReadOnly"
            placeholder="@Placeholder"
           @bind-value="@CurrentValueAsString"
           @bind-value:event="oninput"
            @attributes="AdditionalAttributes">
    </textarea>
}
else
{
    <input type="text"
           class="input bg-dark-550 @Class"
           style="@Style"
           readonly="@IsReadOnly"
           placeholder="@Placeholder"
           @bind-value="@CurrentValueAsString"
           @bind-value:event="oninput"
           @onkeyup="KeyUp"
           @attributes="AdditionalAttributes"/>
}

@code
{
    public enum Variant
    {
        Small,
        Normal,
        Stretch
    }

    [Parameter]
    public Variant Size { get; set; }

    [Parameter]
    public int Rows { get; set; }

    [Parameter]
    public bool IsReadOnly { get; set; }

    [Parameter]
    public string Placeholder { get; set; }

    [Parameter]
    public string Class { get; set; }
    
    [Parameter]
    public Action<KeyboardEventArgs> OnKeyUp { get; set; }

    private string Style => Size switch
    {
        Variant.Small => "width: 80px;",
        Variant.Normal => "",
        Variant.Stretch => "display: block; width: 100%;",
        _ => ""
        };

    protected override bool TryParseValueFromString(string value, out string result, out string validationErrorMessage)
    {
        result = value;
        validationErrorMessage = null;
        return true;
    }

    private void KeyUp(KeyboardEventArgs e)
    {
        OnKeyUp?.Invoke(e);
    }
}